package com.bosssoft.boss_exam_db.service.busines.impl;


import com.bosssoft.boss_exam_db.entity.Filemetadata;
import com.bosssoft.boss_exam_db.mapper.FilemetadataMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.FilemetadataService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <p>
 * 文件存储表 服务实现类
 * </p>
 *
 * @author cherish
 * @since 2018-11-11
 */
@Service
@Slf4j
public class FilemetadataServiceImpl extends BaseServiceImpl<Filemetadata> implements FilemetadataService {

    //对象存储元数据Mapper
    @Autowired
    private FilemetadataMapper filemetadataMapper;

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据条件查询对象存储元数据信息
     * @Param [key, name, page, limit, sort, order] 对象存储KEY、对象存储名称、分页信息
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.Filemetadata>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<Filemetadata> querySelective(String key, String name, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(Filemetadata.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(key)) {
            criteria.andEqualTo("key", key);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", MySqlUtil.cloumLike(name));
        }
        criteria.andEqualTo("deleted",false);

        PageInfo<Filemetadata> pageInfo = PageHelper.startPage(page, limit).setOrderBy(sort + " " + order).doSelectPageInfo(() ->
                this.filemetadataMapper.selectByExample(example)
        );
        return pageInfo;
    }

    @Override
    public String generateUrl(String key) {
        return null;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据对象存储KEY查询对象存储信息
     * @Param [key] 对象存储KEY
     * @Return com.bosssoft.boss_exam_db.entity.Filemetadata
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public Filemetadata findByKey(String key) {
        Example example = new Example(Filemetadata.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("imgKey",key);
        criteria.andEqualTo("deleted",false);
        List<Filemetadata> filemetadataList = filemetadataMapper.selectByExample(example);
        if(filemetadataList!=null && filemetadataList.size()>0){
            return filemetadataList.get(0);
        }
       return null;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据对象存储KEY根据对象存储元数据
     * @Param [filemetadata, key] 对象存储元数据，key
     * @Return void
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public void updateByKey(Filemetadata filemetadata, String key) {
        Example example = new Example(Filemetadata.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(key)) {
            criteria.andEqualTo("key",key);
        }
        criteria.andEqualTo("deleted",false);
        this.filemetadataMapper.updateByPrimaryKeySelective(filemetadata);
    }
}
